package com.gzkit.demo.service.account;

import com.gzkit.baseframework.security.enums.SecurityConstant;
import com.gzkit.baseframework.security.shiro.authe.handler.PostAuthenticationHandler;
import com.gzkit.baseframework.security.shiro.exceptions.PostAuthenticationException;
import com.gzkit.baseframework.security.shiro.provider.AccountProvider;
import com.gzkit.baseframework.security.shiro.token.generator.AuthenticationTokenInfo;
import com.gzkit.baseframework.security.dto.UserAccount;
import com.gzkit.baseframework.sys.organization.vo.SysOrganizationVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 用于认证成功后，进行额外的处理 </br>
 * e.g. 添加额外的账户信息 </br>
 * e.g. 校验用户是否有所属组织 </br>
 * 如果不需要做任何处理，该类可以删除 </br>
 * 下面代码改编自鸭哥经营管理系统，仅做示例用途
 * Created by leodu
 * Created on 2019/12/13
 */
@Service
public class AccountPostHandler implements PostAuthenticationHandler {

    @Autowired
    private AccountProvider accountProvider;

    @Override
    public UserAccount handle(AuthenticationTokenInfo authenticationInfo) throws PostAuthenticationException {
        Subject subject = SecurityUtils.getSubject();
        UserAccount account = subject.getPrincipals().oneByType(UserAccount.class);
        //4a 暂时不做处理
        if(SecurityConstant.LOGIN_TYPE_4A.equals(authenticationInfo.getLoginType())) {
            return account;
        }

        Map<String,Object> extra = account.getExtra();
        //将用户的角色和权限添加到扩展属性里
        String roles = accountProvider.loadRoles(authenticationInfo.getUsername());
        String perms = accountProvider.loadPermissions(authenticationInfo.getUsername());
        extra.put("roles",roles);
        extra.put("perms",perms);

        List<SysOrganizationVO> userOrganizations = accountProvider.loadSysOrganizationByUserName(authenticationInfo.getUsername());
        //当用户未关联组织时，提醒绑定
        if(userOrganizations == null || userOrganizations.size()==0) {
            throw new PostAuthenticationException("该用户没有绑定组织,请联系管理员");
        }
        //当只有一个组织时
        if(userOrganizations.size() == 1)  {
            SysOrganizationVO sysOrganizationVO = userOrganizations.get(0);
            String currentOrgId = sysOrganizationVO.getId();
            extra.put("currentOrgId",currentOrgId);
        }

        return account;
        // todo 当用户绑定多个组织时，要求登录时选择组织，前端暂时未实现该功能
        /*else {
            if(org.springframework.util.StringUtils.isEmpty(loginUser.getOrgId())) {
                return Result.error(userOrganizations);
            }else {
                for(SysOrganizationVO sysOrganizationVO:userOrganizations) {
                    if(org.springframework.util.StringUtils.isEmpty(sysOrganizationVO.getId())
                            || !loginUser.getOrgId().equals(sysOrganizationVO.getId())) {
                        continue;
                    }else {
                        currentOrgId = sysOrganizationVO.getId();
                        currentCompanyId = getCompanyId(sysOrganizationVO);
                    }
                }
                if(org.springframework.util.StringUtils.isEmpty(currentOrgId)) {
                    return Result.error(userOrganizations);
                }
            }
        }*/
    }


//    private String getCompanyId(SysOrganizationVO sysOrganizationVO) {
//        String companyId = "";
//        if(!org.springframework.util.StringUtils.isEmpty(sysOrganizationVO.getOrgType()) && "company".equals(sysOrganizationVO.getOrgType())) {
//            companyId = sysOrganizationVO.getId();
//        }else {
//            //根据所属组织查找公司
//            companyId = accountProvider.loadCompanyByOrgIds(sysOrganizationVO.getParentIds());
//        }
//        return companyId;
//    }

}